package com.lanou3g.code0426;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Created by dllo on 18/4/26.
 */
public class Main {
    /*
    JDBC 一共有4个 核心类
    1.DriverManager
    用来管理驱动的,
    2.Connection
    3.Statement
    4.ResultSet
     */
    public static void main(String[] args) {
        /*
        1.装载驱动
         */
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.获得连接
            Connection conn = DriverManager
                    .getConnection(
                            "jdbc:mysql://localhost:3306/lan_ou",
                            "root",
                            "123456");
            //3.获得执行SQL 语句的对象
            Statement state = conn.createStatement();
            //4.执行SQL语句
            String sql = "select * from stu";
            ResultSet resultSet = state.executeQuery(sql);
            //用来执行  没有结果的sql
            //返回值 int 用来表示 这条sql 影响了多少数据
//            int row =state.executeUpdate(sql);
            while (resultSet.next()) {
                String id= resultSet.getString("sid");
                String name = resultSet.getString("sname");
                String gender= resultSet.getString("gender");
                int age= resultSet.getInt("age");
                System.out.println(resultSet.getObject("sid"));
                System.out.println(resultSet.getObject("sname"));
                System.out.println(resultSet.getObject("gender"));
                System.out.println(resultSet.getObject("age"));
            }
            conn.close();
            state.close();
            //5.关闭资源

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //2.获得连接
}
